package com.common.base.widget.image_watcher;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Handler;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.gif.GifDrawable;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.common.base.R;
import java.io.File;

public class GlideSimpleLoader implements ImageWatcher.Loader {

    private int placeholderId =0;
    private int errorId = 0;

//    public GlideSimpleLoader(){
//        this(R.mipmap.icon_img_infor_defalut, R.mipmap.icon_img_infor_defalut_error);
//    }

    public GlideSimpleLoader(int placeholderId, int errorId){
        this.placeholderId = placeholderId;
        this.errorId = errorId;
    }

    @Override
    public void load(Context context, Uri uri, final ImageWatcher.LoadCallback lc) {
        //本地图片
        if(uri.toString().toLowerCase().startsWith("/storage")){
            loadLocalPic(context, uri, lc);
            return ;
        }
        //网络图片
        loadNetPic(context, uri, lc);
    }

    private void loadLocalPic(Context context, Uri uri, final ImageWatcher.LoadCallback lc){
        if(isGif(uri.toString())){
            loadGif(true, context, uri, lc);
            return ;
        }

        loadImage(true, context, uri, lc);
    }

    private void loadNetPic(Context context, Uri uri, final ImageWatcher.LoadCallback lc){
        if(isGif(uri.toString())){
            loadGif(false, context, uri, lc);
            return ;
        }

        loadImage(false, context, uri, lc);
    }

    private void loadImage(boolean isLocalImg, Context context, Uri uri, final ImageWatcher.LoadCallback lc){
        Glide.with(context)
                .load(isLocalImg ? Uri.fromFile(new File(uri.toString())) : uri)
                .placeholder(placeholderId)
                .error(errorId)
                .into(new SimpleTarget<Drawable>() {
                    @Override
                    public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                        lc.onResourceReady(resource, uri.toString(), false);
                    }
                    @Override
                    public void onLoadFailed(Drawable errorDrawable){
                        lc.onLoadFailed(errorDrawable);
                    }
                    @Override
                    public void onLoadStarted(Drawable placeholder){
                        lc.onLoadStarted(placeholder);
                    }
                });
    }

    private void loadGif(boolean isLocalImg, Context context, Uri uri, final ImageWatcher.LoadCallback lc){
        Glide.with(context)
                .asGif()
                .load(isLocalImg ? Uri.fromFile(new File(uri.toString())) : uri)
                .placeholder(placeholderId)
                .error(errorId)
                .into(new SimpleTarget<GifDrawable>() {
                    @Override
                    public void onResourceReady(@NonNull GifDrawable resource, @Nullable Transition<? super GifDrawable> transition) {
                        lc.onResourceReady(resource, uri.toString(), true);
                    }
                    @Override
                    public void onLoadFailed(Drawable errorDrawable){
                        //在GIF加载异常的时候，进行一次普通的图片加载，避免有些图片被改为GIF后缀造成加载异常
                        Handler handler = new Handler();
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                loadImage(isLocalImg, context, uri, lc);
                            }
                        });
                    }
                    @Override
                    public void onLoadStarted(Drawable placeholder){
                        lc.onLoadStarted(placeholder);
                    }
                });
    }

    private boolean isGif(String url){
        if(TextUtils.isEmpty(url))
            return false;

        return url.contains(".gif") || url.contains(".GIF");
    }

}
